本來是要弄回合時間限制
但發現怎樣算開始呢?
集滿兩勝好像也還沒有說誰獲勝了。
建立遊戲之後,正常的進行遊戲的話,依照順序會有
等等 等待guest 應該不是核心遊戲這層的,在這邊應該只會有兩個
status 可能會有 :start, :host_win, :guest_win
來把它加進初始 Game struct
defstruct host: %{desk: [], hand: initial_hand, wins: 0},
guest: %{desk: [], hand: initial_hand, wins: 0},
turn: 1,
round: 1,
status: :start
檢查勝利的時間點,好像又只能放在出牌後,感覺太多東西在那邊了,
沒關係,先放,有想到更好的再改
應該是接在 |> end_round
後面,來做一個方法檢查有沒有人 wins: 2
def handle_cast({:play_card, player, card}, game) do
game =
game
|> play_card_for(player, card)
|> end_turn()
|> add_wins()
|> end_round()
|> end_game()
{:noreply, game}
end
end_game 方法跟前幾個比單純很多
defp end_game(%{guest: %{wins: 2}} = game), do: Map.replace game, :status, :guest_win
defp end_game(%{host: %{wins: 2}} = game), do: Map.replace game, :status, :host_win
defp end_game(game), do: game
來做一個假的後期遊戲來測
def start_late_game do
late_game = %Game{
guest: %{wins: 1, desk: [3, 2, 1, 3, 2], hand: [1]},
host: %{wins: 1, desk: [6, 3, 3, 5, 2], hand: [2]},
turn: 3,
round: 2
}
GenServer.start_link(__MODULE__, late_game)
end
iex(1)> import Game
Game
iex(2)> {:ok, pid} = start_late_game
{:ok, #PID<0.113.0>}
iex(3)> status pid
%Game{
guest: %{desk: [3, 2, 1, 3, 2], hand: [1], wins: 1},
host: %{desk: [6, 3, 3, 5, 2], hand: [2], wins: 1},
round: 2,
status: :start,
turn: 3
}
iex(4)> play_card pid, :guest, 1
:ok
iex(5)> play_card pid, :host, 2
:ok
iex(6)> status pid
%Game{
guest: %{desk: [3, 2, 1, 3, 2, 1], hand: [], wins: 1},
host: %{desk: [6, 3, 3, 5, 2, 2], hand: [], wins: 2},
round: 3,
status: :host_win,
turn: 1
}
狀態變成獲勝了
明天看要來弄每回合出牌限時
還是修一下就算手上沒有要出的牌,也可以出的情況